1494B - Berland Crossword - CodeForces Solution


bitmasks brute force greedy implementation *1400

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    n, U, R, D, L = map(int, input().split())
    for mask in range(16):
        rU, rR, rD, rL = U, R, D, L
        if mask & 1:
            rU -= 1
            rL -= 1
        if mask & 2:
            rL -= 1
            rD -= 1
        if mask & 4:
            rD -= 1
            rR -= 1
        if mask & 8:
            rR -= 1
            rU -= 1
        if min(rU, rR, rD, rL) >= 0 and max(rU, rR, rD, rL) <= n - 2:
            print("YES")
            break
    else:
        print("NO")

C++ Code:

#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define N 200005
using namespace std;
int arr[N];

bool fun(int num, int n, int m, int k){
      int pos = 0;
      bool flag = false;
      int ct = 0;
      for(int i=1; i<=n; i++){
            if(num%i == 0 and num/i<=m)
                  ct++;
            pos += (num/i);}
      if(ct == 0)return (pos+1)>=k;
      return (pos)>=k;
}
int find(int n){
      int ct = 0;
      while(n){
            ct++;
            n = n&(n-1);
      }
      return ct;
}
int32_t main(){
           int t;
           cin>>t;
           
           while(t--){
                 int n,u,d,r,l;
                 cin>>n>>u>>r>>d>>l;
                 bool flag = false;
                 
                 for(int mask=0; mask<16; mask++){
                       int p,q,x,y;
                       p=u;q=d;x=r;y=l;
                       if((mask & (1<<0))){
                             if(p == 0)continue;
                             p--;
                             if(y == 0)continue;
                             y--;
                       }
                        if((mask & (1<<1))){
                             if(p == 0)continue;
                             p--;
                             if(x == 0)continue;
                             x--;
                       }
                        if((mask & (1<<2))){
                             if(q == 0)continue;
                             q--;
                             if(x == 0)continue;
                             x--;
                       }
                        if((mask & (1<<3))){
                             if(q == 0)continue;
                             q--;
                             if(y == 0)continue;
                             y--;
                       }
                       
                       if(p<=n-2 and q<=n-2 and x<=n-2 and y<=n-2)
                        flag = true;
                       
                 }
                 
                 if(flag)          cout<<"YES"<<endl;
                 else cout<<"NO"<<endl;
           }
        }
            // (i,j)
    
   // 1 + 1 + 2 + 4 + 8
//   1 4 -> 


Comments

Submit
0 Comments
More Questions

1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals